﻿Public Class F_NhanVien
    Dim action As Integer = 1
    Dim nv As New DTONhanVien
    Dim tk As New DTOTaiKhoan
    Sub recieveData(ByVal n As DTONhanVien, ByVal a As Integer)
        txtDateNgaySinh.Text = n.NgaySinh
        txtTennv.Text = n.TenNV
        txtDiachi.Text = n.DiaChi
        txtSodienthoai.Text = n.DienThoai
        cbxChucVu.SelectedValue = n.MaCV
        nv.MaNV = n.MaNV
        tk.MaNV = n.MaNV
        Dim sql As String = "select Username from TaiKhoan where MaNV = " & n.MaNV
        tk.Username = Convert.ToString(DataProvider.ExecuteScalar(sql))
        txtUsername.Text = tk.Username
        action = a
    End Sub
    Sub getDataInForm()
        nv.TenNV = txtTennv.Text
        nv.GioiTinh = cbbGioitinh.Text
        nv.NgaySinh = Convert.ToDateTime(txtDateNgaySinh.Text)
        nv.MaCV = cbxChucVu.SelectedValue
        nv.DiaChi = txtDiachi.Text
        nv.DienThoai = txtSodienthoai.Text
        tk.Username = txtUsername.Text
    End Sub
    Sub loadChucVu()
        cbxChucVu.DataSource = DataProvider.LoadTable("select * from chucvu")
        cbxChucVu.ValueMember = "MACV"
        cbxChucVu.DisplayMember = "TenCV"
        cbxChucVu.SelectedIndex = 1
    End Sub
    Private Sub F_NhanVien_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadChucVu()
        cbbGioitinh.SelectedIndex = 1
        If action = 1 Then
            txtDateNgaySinh.Text = DateTime.Now
            txtUsername.Enabled = False
        Else
            txtUsername.Enabled = True
        End If
    End Sub
    Function checkEmpty() As Boolean
        If txtDateNgaySinh.Text = "" Or _
        txtTennv.Text = "" Or _
        txtDiachi.Text = "" Or _
            txtSodienthoai.Text = "" or txtUsername.Text = "" Then
            Return False
        End If
        Return True
    End Function

    Sub btnGhidulieu_Click(sender As Object, e As EventArgs) Handles btnGhidulieu.Click
        getDataInForm
        If action = 0 Then
            tk.Password = "123"
            BUSNHANVIEN.NhanVien_Update(nv)
            tk.MaNV = nv.MaNV
            BUSTaiKhoan.TaiKhoan_Update(tk)
            action = 1
        Else

            If checkEmpty() = False Then
                MsgBox("Dữ liệu chưa nạp đủ !")
                Return
            End If
            '  tk.Password = "123"
            '  tk.Username = txtUsername.Text
            '  tk.MaNV = nv.MaNV
            BUSNHANVIEN.NhanVien_Insert(nv)
            'BUSTaiKhoan.TaiKhoan_Insert(tk)
        End If
        Me.Close()
    End Sub
End Class